package com.czapl.drawing;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;

public class AnimatedSprite extends Sprite
{
   protected final TextureAtlas mTextureAtlas;
   
   protected AtlasRegion mFrame[];
   protected int mCurFrame;
   
   protected float mAnimationRateInSeconds;
   protected float mFrameTimeCounter;
   protected boolean mIsPlaying;
   protected boolean mLoop;
   
   protected boolean mFlipX;
   protected boolean mFlipY;
   
   public AnimatedSprite(final TextureAtlas textureAtlas, final String regionName, final int frameCount)
   {
      super( textureAtlas.findRegion( regionName, 1 ) );
      mTextureAtlas = textureAtlas;
      
      mAnimationRateInSeconds = 1.0f;
      mIsPlaying = false;
      mLoop = true;
      
      mFlipX = false;
      mFlipY = false;
      
      mFrame = new AtlasRegion[ frameCount ];
      for( int ii=0; ii<frameCount; ++ii )
      {
         mFrame[ii] = mTextureAtlas.findRegion( regionName, ii+1 );
      }
      
      mCurFrame = 0;
   }
   
   public int getFrameCount() {
	   return mFrame.length;
   }
   
   public void setAnimationRate(final int fps)
   {
      mAnimationRateInSeconds = 1.0f / (float)fps;
   }
   
   public void loop( boolean doLoop )
   {
      mLoop = doLoop;
   }
   public final boolean looping()
   {
      return mLoop;
   }
   
   public void restart()
   {
      mIsPlaying = true;
      mFrameTimeCounter = 0;
      mCurFrame = 0;
   }
   
   public void play()
   {
	   if(!mIsPlaying) {
		   mIsPlaying = true;
		   mFrameTimeCounter = 0;
	   }
   }
   
   public void pause()
   {
	   if(mIsPlaying) {
		   mIsPlaying = false;
		   mFrameTimeCounter = 0;
	   }
   }
   
   public void stop()
   {
      pause();
      mCurFrame = 0;
   }
   
   public void update(final float secondsElapsed)
   {
      if( mIsPlaying )
      {
         mFrameTimeCounter += secondsElapsed;
         if(mFrameTimeCounter > mAnimationRateInSeconds)
         {
            mFrameTimeCounter = 0;
            ++mCurFrame;
            
            // Back to first
            if( mCurFrame >= mFrame.length )
            {
               // If not looping, stop
               if( !mLoop )
               {
                  stop();
               }
               // Other wise just reset
               else
               {
                  mCurFrame = 0;
               }
            }
            
            setRegion( mFrame[ mCurFrame ] );
         }
      }
   }
   
   public void setRegion (TextureRegion region)
   {
      super.setRegion( region );
      super.flip( mFlipX, mFlipY );
   }
   
   public void flip( boolean x, boolean y )
   {
      mFlipX = x;
      mFlipY = y;
      super.flip( mFlipX, mFlipY );
   }
}